package com.yenlien.traceyun.plant.component;


import cn.hutool.http.HttpUtil;
import com.yenlien.traceyun.plant.dao.FarmDao;
import com.yenlien.traceyun.plant.dao.PlantNewDao;
import com.yenlien.traceyun.plant.entity.Farm;
import com.yenlien.traceyun.plant.entity.PlantNews;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import static com.yenlien.traceyun.plant.utils.Tools.DATE_TIME_FORMATTER;

@Component
public class FarmAlarm {

    @Autowired
    private FarmDao farmDao;
    @Autowired
    private PlantNewDao plantNewDao;

    @Scheduled(fixedRate = 1000 * 60)
    public void getData() {
        LocalDate now = LocalDate.now();
        List<Farm> farmList = farmDao.findByAlarm();
        for (Farm farm : farmList) {
            if(farm.isAlarmFlag()){
                LocalDate endTime = LocalDate.parse(farm.getEndTime());
                if(endTime.isBefore(now) && !farm.isFinished()){
                    // 查看是否已添加该农事的预警
                    String farmId = plantNewDao.checkExistByCorrelationId(farm.getId(), "1");
                    if(StringUtils.isBlank(farmId)){
                        PlantNews plantNews = new PlantNews();
                        plantNews.setCompanyId(farm.getCompanyId());
                        plantNews.setCorrelationId(farm.getId());
                        plantNews.setReadState(false);
                        plantNews.setType("1");
                        plantNews.setCreateTime(LocalDateTime.now().format(DATE_TIME_FORMATTER));
                        plantNews.setContent(farm.getPrincipalName() + " 您的农事任务: " + farm.getType() + "已超出规定时间: " + farm.getEndTime() + ", 请尽快完成");
                        plantNewDao.save(plantNews);
                    }
                }
            }

            if(farm.isNoticeFlag()){
                LocalDate beginTime = LocalDate.parse(farm.getBeginTime());
                if(beginTime.isBefore(now) && !farm.isFinished() && beginTime.isAfter(now.minusDays(farm.getNoticeDay()))){
                    // 查看是否已添加该农事的预警
                    String farmId = plantNewDao.checkExistByCorrelationId(farm.getId(), "2");
                    if(StringUtils.isBlank(farmId) ){
                        PlantNews plantNews = new PlantNews();
                        plantNews.setCompanyId(farm.getCompanyId());
                        plantNews.setCorrelationId(farm.getId());
                        plantNews.setReadState(false);
                        plantNews.setType("2");
                        plantNews.setCreateTime(LocalDateTime.now().format(DATE_TIME_FORMATTER));
                        plantNews.setContent(farm.getPrincipalName() + "距离您的农事任务:" + farm.getType() + "还剩" + farm.getNoticeDay() + "天开始实施");
                        plantNewDao.save(plantNews);
                    }
                }
            }
        }
    }






    public static void main(String[] args) {
        String url = "https://restapi.amap.com/v3/weather/weatherInfo?city=610400&key=fc496c9eee0ef60df95a36da9df5e7b5";






        System.out.println(HttpUtil.get(url));

    }


}
